package com.notes.romania.browser;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.notes.romania.R;
import com.notes.romania.arrayadapter.NotificationArrayAdapter;
import com.notes.romania.reminder.RemManager;
import com.notes.romania.reminder.Reminder;

public class NotificationBrowser extends Activity {
	private ArrayList<String> title;
	private ArrayList<String> text;
	private ArrayList<Integer> idd;
	
	private NotificationArrayAdapter adapter;
	
	private ListView listView;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.notification_browser);
		
		SharedPreferences settings = getApplicationContext().getSharedPreferences(RemManager.PREFS_NAME, Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = settings.edit();
    	RemManager.setUp(settings,  editor);
    	
		title = new ArrayList <String>();
		text = new ArrayList <String>();
		idd = new ArrayList <Integer>();
		
		adapter = new NotificationArrayAdapter(this, new ArrayList<String>(), new ArrayList<String>());
		listView = (ListView)findViewById(R.id.listView2);
		listView.setAdapter(adapter);
		
		readReminders();
		updateList();
		
		listView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				final AlertDialog alertDialog = new AlertDialog.Builder(NotificationBrowser.this).create();
		        alertDialog.setTitle(title.get(position));
		        alertDialog.setMessage(text.get(position));
		        alertDialog.setIcon(R.drawable.reminder_icon);
		       
		        alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int which) {
		            	Reminder rem = RemManager.readRem(idd.get(position));
		            	
		            	RemManager.removeRem(rem);
		            	
		            	readReminders();
		            	updateList();
		            }
		        });
		        alertDialog.show();
			}});
	}
	
	void readReminders(){
		title.clear();
		text.clear();
		idd.clear();
		
		//Reminder
		Reminder rem = null;
		int remInMemory = RemManager.getListSize(), i;
		
		//Get a list with reminders id
		int ids[] = new int[remInMemory + 1];
		for(i = 1; i <= remInMemory; i ++)
			ids[i] = RemManager.getIdAtIndex(i);
			
		for(i = 1; i <= remInMemory; i ++){
			rem = RemManager.readRem(ids[i]);
			title.add(rem.day + "/" + rem.month + "/" + rem.year + " " + rem.hour + ":" + rem.minute);
			text.add(rem.text);
			idd.add(rem.id);
		}
	}
	
	void updateList(){
		adapter.clear();
		adapter.addText(text);
		adapter.addAll(title);
	}
	
} 